import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/models/authorization_community_entity.dart';
import 'package:kkhousekeep/page/authorization/authorization_page_controller.dart';
import 'package:kkhousekeep/widget/custom_app_bar.dart';

class BuildAuthorizationPage extends StatelessWidget {
  const BuildAuthorizationPage({super.key, this.isJumpToMePage = false});

  final bool isJumpToMePage;

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: AuthorizationController(isJumpToMePage: isJumpToMePage),
      builder: (controller) => GestureDetector(
        onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
        child: Scaffold(
          resizeToAvoidBottomInset: false,
          appBar: CustomAppBar.leftTitle(
            title: "请选择你的社区",
            callback: () => controller.processBack(),
          ),
          body: ConstrainedBox(
            constraints: const BoxConstraints.expand(),
            child: Container(
              decoration: const BoxDecoration(color: Color(0xFFF4F5F6)),
              child: Column(
                children: [
                  SizedBox(
                    height: 16.h,
                  ),
                  _buildSearchWidget(controller),
                  _buildCommunityList(controller)
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  ///搜索组件
  Widget _buildSearchWidget(AuthorizationController controller) {
    return Container(
      height: 104.h,
      width: 1.sw,
      padding: EdgeInsets.only(left: 32.h, right: 32.h),
      decoration: const BoxDecoration(color: Colors.white),
      child: Flex(
        mainAxisSize: MainAxisSize.min,
        direction: Axis.horizontal,
        children: [
          Expanded(
              flex: 33,
              child: InkWell(
                onTap: () => controller.buildCityPage(),
                child: Row(
                  children: [
                    Image.asset(
                      "assets/images/authorization_location_icon.png",
                      height: 48.h,
                      width: 48.h,
                    ),
                    Obx(
                      () => Text(
                        controller.city.value,
                        style: TextStyle(fontSize: 32.sp),
                      ),
                    ),
                    SizedBox(
                      width: 14.w,
                    ),
                    Image.asset(
                      "assets/images/authorization_expand_icon.png",
                      width: 32.h,
                      height: 32.h,
                    ),
                  ],
                ),
              )),
          Expanded(
              flex: 67,
              child: ConstrainedBox(
                constraints: BoxConstraints(maxHeight: 60.h),
                child: TextField(
                  onChanged: (text) async =>
                      controller.handleCommunitiesResponse(keyword: text),
                  style: TextStyle(fontSize: 28.sp),
                  controller: controller.searchTextController,
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.only(top: 16.h),
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.all(
                        Radius.circular(36.w),
                      ),
                      borderSide:
                          const BorderSide(style: BorderStyle.none, width: 0),
                    ),
                    filled: true,
                    fillColor: const Color(0xFF1774E0).withOpacity(0.06),
                    prefixIcon: Container(
                        padding: EdgeInsets.only(
                          bottom: 13.h,
                          left: 23.h,
                          right: 13.h,
                          top: 13.h,
                        ),
                        child: Image.asset(
                          "assets/images/authorization_search_icon.png",
                        )),
                    suffixIcon: Container(
                      padding: EdgeInsets.only(
                        left: 40.h,
                        top: 12.h,
                        bottom: 12.h,
                      ),
                      child: InkWell(
                        onTap: () => controller.clearText(),
                        child: Image.asset(
                          "assets/images/common_clear.png",
                          width: 36.h,
                          height: 36.h,
                        ),
                      ),
                    ),
                    hintText: "请输入您的小区",
                    hintStyle: TextStyle(
                      fontSize: 28.sp,
                      color: const Color(0xFF999999),
                    ),
                  ),
                ),
              )),
        ],
      ),
    );
  }

  ///社区列表组件
  Widget _buildCommunityList(AuthorizationController controller) {
    return Obx(
      () => controller.isCommunityEmpty.value
          ? Expanded(
              child: Container(
              decoration: const BoxDecoration(
                color: Color(0xFFF4F5F6),
              ),
              child: Stack(
                alignment: Alignment.center,
                children: [
                  Positioned(
                      top: 272.h,
                      child: Image.asset(
                        "assets/images/authorization_community_not_found.png",
                        width: 288.w,
                        height: 358.w,
                      )),
                  Positioned(
                    top: 638.h,
                    child: Text(
                      "没有找到你的社区",
                      style: TextStyle(
                        fontSize: 28.sp,
                        color: const Color(0xFF999999),
                      ),
                    ),
                  )
                ],
              ),
            ))
          : Expanded(
              child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Visibility(
                  visible: controller.isShowTip.value,
                  child: Container(
                    padding: EdgeInsets.only(
                      left: 32.w,
                      right: 36.w,
                    ),
                    height: 84.h,
                    alignment: Alignment.centerLeft,
                    decoration: const BoxDecoration(
                      color: Color(0xFFF4F5F6),
                    ),
                    child: Text(
                      "全部社区",
                      style: TextStyle(
                          fontSize: 32.sp, fontWeight: FontWeight.w500),
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    decoration: const BoxDecoration(
                      color: Color(0xFFF4F5F6),
                    ),
                    child: ListView.builder(
                      itemBuilder: (BuildContext context, int index) {
                        return _buildSingleList(
                            controller.communityList.elementAt(index),
                            controller);
                      },
                      itemCount: controller.communityList.length,
                      shrinkWrap: true,
                      itemExtent: 96.h,
                    ),
                  ),
                ),
              ],
            )),
    );
  }

  ///列表子项
  Widget _buildSingleList(
      Community community, AuthorizationController controller) {
    return InkWell(
      onTap: () => controller.buildRoomPage(community),
      child: Container(
        color: Colors.white,
        padding: EdgeInsets.only(left: 32.w, right: 32.w),
        child: Container(
          decoration: BoxDecoration(
            border: Border(
              bottom: BorderSide(
                width: 1.h,
                color: const Color(0xFFF4F5F6),
              ),
            ),
          ),
          child: Flex(
            direction: Axis.horizontal,
            children: [
              Expanded(
                flex: 92,
                child: Text(
                  community.communityName,
                  style: TextStyle(fontSize: 32.sp),
                ),
              ),
              Expanded(
                flex: 8,
                child: Image.asset(
                  "assets/images/authorization_jump_icon.png",
                  width: 48.h,
                  height: 48.h,
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
